<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .boss {
      width: 600px;
      height: 400px;
      background-color: black;
      position: absolute;
      left: 50px;
      top: 50px;
      /* display: none; */
      background-image: url(./a1.jpg);
      background-size: 100% 100%;
      background-repeat: no-repeat;
    }

    /* .boss .img {
      width: 100%;
      height: 400px;
    } */

    .boss .bossOne {
      width: 50px;
      height: 50px;
      background-color: #fff;
      position: absolute;
      top: 200px;
      left: 400px;
    }

    .boss .bossTwo {
      width: 50px;
      height: 50px;
      background-color: yellow;
      background-image: url(./a1.jpg);
      background-repeat: no-repeat;
      background-size: 600px auto;
      background-position: -400px -200px;
      position: absolute;
      top: 200px;
      left: 0;
    }


    .box {
      width: 600px;
      height: 50px;
      background-color: gray;
      position: absolute;
      left: 50px;
      top: 400px;
      opacity: 0.6;
    }

    .round {
      width: 50px;
      height: 50px;
      background-color: #fff;
      border-radius: 50%;
      position: absolute;
      /* left: 50px; */
      /* top: 500px */
    }
  </style>
</head>

<body>
  <div class="boss">
    <div class="bossOne"></div>
    <div class="bossTwo"></div>
  </div>
  <div class="box">
    <div class="round"></div>
  </div>
</body>
<script>
  var boss = document.getElementsByClassName('boss')[0];
  var box = document.getElementsByClassName('box')[0];
  var round = document.getElementsByClassName('round')[0];
  var bossOne = document.getElementsByClassName('bossOne')[0];
  var bossTwo = document.getElementsByClassName('bossTwo')[0];


  var maxLeft = box.clientWidth - round.clientWidth
  var maxTop = boss.clientHeight - round.clientHeight - box.clientHeight;

  randPos();

  function randPos() {
    var randX = 200 + Math.round(Math.random() * (maxLeft - 200));
    var randY = 50 + Math.round(Math.random() * (maxTop - 100));

    // bossOne随机位置
    bossOne.style.left = randX + "px";
    bossOne.style.top = randY + "px";


    bossTwo.style.left = 0 + "px";
    bossTwo.style.top = randY + "px";

    bossTwo.style.backgroundPositionX = (-randX) + "px";
    bossTwo.style.backgroundPositionY = (-randY) + "px";
  }


  box.onmousedown = function (e) {
    // console.log(1111);
    var e = e || window.event;
    console.log(e);

    var posX = e.offsetX;
    // var posY = e.offsetY;
    // console.log(posX, posY);


    document.onmousemove = function () {
      var e = e || window.event;
      var x = e.pageX - posX - boss.offsetLeft;
      // var y = e.pageY - posY;
      if (x < 0) {
        x = 0;
      }
      if (x >= maxLeft) {
        x = maxLeft;
      }



      round.style.left = x + "px";
      bossTwo.style.left = x + "px";
      console.log('1', round.offsetLeft, '2', bossTwo.offsetLeft, '3', bossOne.offsetLeft);

    }

    document.onmouseup = function () {
      document.onmousemove = null;
      // [bossTwo.offsetLeft-2,bossTwo.offsetLeft+2]
      if (bossTwo.offsetLeft) {
        if (bossTwo.offsetLeft <= (bossOne.offsetLeft + 10) && bossTwo.offsetLeft >= (bossOne.offsetLeft - 10)) {
          // boss.style.display = 'block';
          window.open("http://www.baidu.com");
          // boss.style.background = 'red'
        } else {
          round.style.left = 0 + 'px';
          bossTwo.style.left = 0 + "px";
          randPos();
        }
      }

    }

  }
</script>

</html>